#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QFontDialog>

SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);

    initActions();
    initObject();
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::initActions()
{
    connect(ui->applyButton, &QPushButton::clicked,this, &SettingsDialog::apply);

}

void SettingsDialog::initObject()
{
    this->setModal(true);//设置为阻塞
    settingManage = new SettingManage();
    settings = settingManage->getSettings();

    QString rootPath = settings->value("Notes/RootPath", "NOTES_DATA").toString();

    //connect(ui->applyButton, &QPushButton::clicked, this, &SettingsDialog::apply);
    ui->comboBox->addItem(tr("QPlainTextEdit"));
    ui->comboBox->addItem(tr("QTextEdit"));
    ui->comboBox->addItem(tr("QsciScintilla"));

    ui->comboBox_2->addItem(rootPath);
    QLabel *listFontLabel = ui->listFontLabel;

    QString listFontFamily = settings->value("List/fontFamily", "Microsoft YaHei UI").toString();
    bool listFontBold = settings->value("List/fontBold", false).toBool();
    int listFontSize = settings->value("List/fontSize", 12).toInt();

    QString txt = QString("%1 %2 %3").arg(listFontFamily).arg(listFontBold ? "true" : "false").arg(listFontSize);
    listFontLabel->setText(txt);
}

void SettingsDialog::apply()
{
    hide();
}

void SettingsDialog::on_applyButton_clicked()
{
    //hide();
    qDebug() << "on_applyButton_clicked!!";
}


void SettingsDialog::on_getFontButton_clicked()
{
    QFont listFont;

    QString listFontFamily = settings->value("List/fontFamily", "Microsoft YaHei UI").toString();
    bool listFontBold = settings->value("List/fontBold", false).toBool();
    int listFontSize = settings->value("List/fontSize", 12).toInt();

    listFont.setFamily(listFontFamily);
    listFont.setBold(listFontBold);
    listFont.setPointSize(listFontSize);

    bool fontSelected;
    QFont font = QFontDialog::getFont(&fontSelected, listFont, this);
    if (fontSelected) {
        QLabel *listFontLabel = ui->listFontLabel;
        QString txt = QString("%1 %2 %3").arg(font.family()).arg(font.bold() ? "true" : "false").arg(font.pointSize());

        settings->setValue("List/fontFamily", font.family());
        settings->setValue("List/fontSize", font.pointSize());
        settings->setValue("List/fontBold", font.bold());

        listFontLabel->setText(txt);
    }
}
